package com.uuah.server.config.registry;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.server.api.config.registry.IServiceDescriptor;
import com.uuah.utils.ClassUtils;
import com.uuah.utils.StringUtils;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 上午10:02:15
 * @version 1.0.0
 */
public abstract class AbstractServiceDescriptor implements IServiceDescriptor {

	protected transient final Logger logger = LoggerFactory
			.getLogger(getClass());

	protected String service;

	public AbstractServiceDescriptor(String service) {
		this.service = service;
	}

	public String getService() {
		return service;
	}

	protected String removeProperty(String name, Properties properties) {
		String temp = (String) properties.remove(name);
		if (StringUtils.isEmpty(StringUtils.trim(temp))) {
			return null;
		} else {
			return temp;
		}
	}

	protected Class removeClassProperty(String name, Properties properties)
			throws ClassNotFoundException {
		String clazz = removeProperty(name, properties);
		if (clazz == null) {
			return null;
		} else {
			return ClassUtils.loadClass(clazz, getClass());
		}
	}

	public static class Key {

		private final String service;

		public Key(String service) {
			this.service = service;
		}

		// @Override
		public boolean equals(Object o) {
			if (this == o) {
				return true;
			}
			if (!(o instanceof Key)) {
				return false;
			}

			final Key key = (Key) o;

			if (!service.equals(key.service)) {
				return false;
			}

			return true;
		}

		// @Override
		public int hashCode() {
			return (service != null ? service.hashCode() : 0);
		}

		public String getKey() {
			return service + ":" + Integer.toString(hashCode());
		}

	}

}
